<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>
    <script>
        function sum(a, b) {
            return a + b;
        }

        /* 最原始的写法 */
        // function validatorFunction(func) {
        //     return new Proxy(func, {
        //         apply(target, thisArgument, argumentsList){
        //             return Reflect.apply(target, thisArgument, argumentsList)
        //         }
        //     })
        // }

        /* 校验传递参数的写法 */
        function validatorFunction(func,...types) {
            return new Proxy(func, {
                apply(target, thisArgument, argumentsList){
                    types.forEach((type,i)=>{
                        if (typeof argumentsList[i] !== type) {
                            throw new TypeError(`第${i+1}个参数${argumentsList[i]}不满足不满足类型${type}`);
                        }
                    })
                    return Reflect.apply(target, thisArgument, argumentsList)
                }
            })
        }

        const sumProxy = validatorFunction(sum,'number','number')
        sumProxy(1,2)

    </script>
</body>

</html>